package com.icss.clothes.cotroller;

import com.icss.clothes.entity.Remark;
import com.icss.clothes.entity.User;
import com.icss.clothes.service.imp.RemarkService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 关于评论的控制器
 */
@Controller
public class RemarkController {
    @Autowired
    private RemarkService remarkService;

    //添加评论
    @RequestMapping("/addRemark")
    public ModelAndView addRemark(Remark remark, HttpSession session, MultipartFile remark_msg) {
        ModelAndView modelAndView=new ModelAndView("index");
        User user = (User) session.getAttribute("loginUser");
        if (user==null) {
            modelAndView.setViewName("login");
            modelAndView.addObject("msg", "请先登录！");
            return modelAndView;
        }
        try {
            String path="D:\\IdeaIU\\Clother_shopping\\commodity\\remark";
            String filename=remark_msg.getOriginalFilename();
            File targetFile=new File(path+"\\"+filename); //指定要保存的文件对象
            remark_msg.transferTo(targetFile);//把当前的uploadHead文件保存到参数中的指定文件下
            remark.setRemark_picture(filename); //文件上传之后，上传路径到数据库
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        remark.setUser_id(user.getUser_id());
        String msg=remarkService.addRemark(remark);
        modelAndView.addObject("msg",msg);
        return modelAndView;
    }
    //评论区展示
    @RequestMapping("/showRemark")
    @ResponseBody
    public List<Remark> showRemark(int c_id) {
        List<Remark> remarkList = remarkService.selectRemarkByCid(c_id);
        return remarkList;
    }
}
